/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test.annotated.parent;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Base64;
import org.sourceprojects.lycia.annotations.Parent;
import org.sourceprojects.lycia.annotations.TextBody;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.sourceprojects.lycia.test.TestCaseUtilities;

import static org.sourceprojects.lycia.fluent.FluentBuilder.*;
import static org.junit.Assert.*;

public class ParentAnnotationExtendedInjectionTestCase {

	private static final String XML_SIMPLE = TestCaseUtilities.XML_HEADER
			+ "<outertest><test id=\"test\"><testcase-simple>dGVzdA==</testcase-simple>Hello</test>World</outertest>";

	private static final String XML_SIMPLE_BASE64 = TestCaseUtilities.XML_HEADER
			+ "<outertest><test id=\"test\"><testcase-simple>dGVzdA==</testcase-simple>SGVsbG8=</test>V29ybGQ=</outertest>";

	private final Object contextObject = new Object();

	@Test
	public void testAnnotationExtendedInjectionNullUnaware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(false),
				contextObject(contextObject)).parser(
				tag("testcase-simple", method("annotationExtendedInjection",
						this, String.class, byte[].class, String.class,
						byte[].class))).build();

		parser.parse(XML_SIMPLE);
	}

	@Test
	public void testAnnotationExtendedInjectionNullAware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(
				contextObject(contextObject)).parser(
				tag("testcase-simple", method("annotationExtendedInjection",
						this, String.class, byte[].class, String.class,
						byte[].class))).build();

		parser.parse(XML_SIMPLE);
	}

	@Test
	public void testBase64AnnotationExtendedInjectionNullUnaware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(false),
				contextObject(contextObject))
				.parser(
						tag("testcase-simple", method(
								"base64AnnotationExtendedInjection", this,
								String.class, byte[].class, String.class,
								byte[].class))).build();

		parser.parse(XML_SIMPLE_BASE64);
	}

	@Test
	public void testBase64AnnotationExtendedInjectionNullAware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(
				contextObject(contextObject))
				.parser(
						tag("testcase-simple", method(
								"base64AnnotationExtendedInjection", this,
								String.class, byte[].class, String.class,
								byte[].class))).build();

		parser.parse(XML_SIMPLE_BASE64);
	}

	public void annotationExtendedInjection(@Parent @TextBody String textBody,
			@Parent @TextBody byte[] textBodyArray,
			@Parent(layers = 2) @TextBody String layeredTextBody,
			@Parent(layers = 2) @TextBody byte[] layeredTextBodyArray) {

		assertNotNull(textBody);
		assertEquals("Hello", textBody);

		assertNotNull(textBodyArray);
		assertEquals("Hello", new String(textBodyArray));

		assertNotNull(layeredTextBody);
		assertEquals("World", layeredTextBody);

		assertNotNull(layeredTextBodyArray);
		assertEquals("World", new String(layeredTextBodyArray));
	}

	public void base64AnnotationExtendedInjection(
			@Parent @TextBody @Base64 String textBody,
			@Parent @TextBody @Base64 byte[] textBodyArray,
			@Parent(layers = 2) @TextBody @Base64 String layeredTextBody,
			@Parent(layers = 2) @TextBody @Base64 byte[] layeredTextBodyArray) {

		assertNotNull(textBody);
		assertEquals("Hello", textBody);

		assertNotNull(textBodyArray);
		assertEquals("Hello", new String(textBodyArray));

		assertNotNull(layeredTextBody);
		assertEquals("World", layeredTextBody);

		assertNotNull(layeredTextBodyArray);
		assertEquals("World", new String(layeredTextBodyArray));
	}

}
